<template>
  <div>
      <div class="logo-header">
        <img class="logo-view" src="../../assets/login/c-logo.png" />
      </div>
    <div class="login-container">
     <img class="logo-st" src="../../assets/login/002.png" />
         <img class="logo-srt" src="../../assets/login/001.png" />
      <el-form ref="loginForm" 
      :model="loginForm" 
      :rules="loginRules" 
      class="login-form" 
      autocomplete="on" 
      label-position="left">

        <div class="title-container">
          欢迎登录
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入登录账户"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入登录密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.enter="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <!-- <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span> -->
          </el-form-item>
        </el-tooltip>
        <div>
            <el-button :loading="loading" type="primary" class="btn-login"
          @click.native.prevent="handleLogin">登录</el-button>
        </div>

      </el-form>
    </div>

        <div class="footer-view">
          <div style="padding-bottom:15px">版权归：重庆探程数字科技有限公司</div>
          <div>
             <span>网站:<a href="http://www.cqtancheng.com" target="_blank">www.cqtancheng.com</a>  </span>
            <span> 电话：023-63615762     </span>
             <span> 网站备案：<a target="_blank" href="https://beian.miit.gov.cn/state/outPortal/loginPortal.action">渝ICP备2020014689号-1</a>  </span>
             
             </div>
        </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { removeToken } from '@/utils/auth'
import Cookies from 'js-cookie'
import {sendCode,verifyCode,changePSW} from '@/api/clientManage'
import { clearInterval } from 'timers';

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入登录账户'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入登录密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        loginType:'boss'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    removeToken()
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then((res) => {
              this.loading = false
              if(res.success){
                localStorage.setItem('menuKey',1)
                this.$router.push({ path: '/home', query: this.otherQuery })
              }else{
                this.$message.error(res.message||"操作失败");
              }
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
.login-container {
  background-image: url('../../assets/login/login-bg.jpg');
  background-position: top;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 45px;
    line-height: 45px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
    }
  }
  .el-form-item {
    background: #f5f5f5;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.dialog-fade-enter-active {
  animation: anim-open .5s !important;
}
@keyframes anim-open {
  0%{
    opacity: 0;
    transform:translateY(300px)
  }
    100%{
    opacity: 1;
    transform:translateY(0)
  }
}
.dialog-fade-leave-active {
   animation: anim-close 1s !important;
}
@keyframes anim-close {
  0%{
    opacity: 1;
    transform:translateY(0)
  }
    100%{
    opacity: 0;
    transform:translateY(300px)
  }
}
.logo-st{
    height: 500px;
    position: absolute;
    top: 50%;
    margin-top: -250px;
    z-index: 1;
    left: 10%; 
}
.logo-srt{
    height: 500px;
    position: absolute;
    top: 50%;
    margin-top: -250px;
    z-index: 1;
    right: 10%; 
}
.wx-pupop-title{
    font-size: 15px;
    text-align: center;
    color: #424242;
    padding: 20px 0;
}
.wx-pupop-image{
  text-align: center;
    width: 100%;
    padding-bottom:20px;
    img{
      width:80%;
    }
}
  .logo-header{
    height:70px;
    margin:0;
    background-color: #fff;
    box-shadow: rgba(145, 145, 145, 0.17) 0px 3px 6px 0px;
    width:100%;
    position: relative;
      .logo-view{
        height:60px;
        position: absolute;
        left:20px;
        top:50%;
        margin-top:-30px;
      }
  }
.login-container {
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  position: relative;
  height: calc(100vh - 170px);
  min-width:1000px;
  min-height:calc(100vh - 170px);
  .center-login{
    width:500px;
    height:100px;
    text-align: center;
    margin:0 auto;
    position: absolute;
    left:50%;
    margin-left:-250px;
    top:160px;
    display: flex;
    justify-content: space-between;
    img{
      height:63px;
      width:228px;
    }
  }
  .login-form {
    position: absolute;
    width: 400px;
    height:400px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
    right:15%;
    top:50%;
    margin-top:-200px;
    padding:30px;
    border-radius:6px;
    z-index:9;
  }
.btn-login{
    width: 100%;
    margin-bottom: 20px;
    margin-top: 10px;
    background-color: #30ab74 !important;
    border-color: #30ab74 !important;
    height: 40px;
}
.btn-wxlogin{
    width: 100%;
    margin-bottom:10px;
    margin-top: 0;
    background-color: #fff !important;
    border-color: #30ab74 !important;
    color:#30ab74 !important;
    height: 40px;
    .wxlogin{
          display: flex;
        justify-content: center;
        align-items: center;
    }
}

.forget-pass{
      text-align: center;
    font-size: 13px;
    color: #888;
    padding-top: 10px;
    cursor: pointer;
}

  .tip-view{
    font-size:14px;
    color:#565656;
    line-height:25px;
    position: relative;
    .down{
      position: absolute;
      font-size:14px;
      right:0;
      color:#0777df;
      font-weight: bold;
      bottom:0;
      text-decoration:underline
    }
  }
  .svg-container {
    padding: 0 5px 0 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    font-size: 20px;
    color: #30ab74;
    margin: 0px auto 40px auto;
    text-align: left;
    font-weight: bold;
    border-bottom:1px solid #30ab74;
    line-height: 50px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  .el-button--primary {
      color: #FFFFFF;
      background-color: #0777df;
      border-color: #1890ff;
  }
  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }

}
.btn-next{
      width: 100%;
    margin-bottom: 20px;
    margin-top: 30px;
    background-color: #30ab74 !important;
    border-color: #30ab74 !important;
    height: 40px;
}
  .footer-view{
    background-color: #2e2e41;
    height: 100px;
    width: 100%;
    text-align: center;
    color: #fff;
    font-size: 15px;
    padding-top:30px;
    span{
      padding:0 10px;
    }
  }
</style>
